<template>
	<div>
		<navigationBar :backType="2" navigationTitle="服务详情" />

		<view class="wrap paddingBottom">
			<banner bannerHeight="562rpx" :bannerList="dataInfo.images" :pageType="1" position="right" />

			<view class="wrap-content">
				<view class="title f-36 fBold"> {{ dataInfo.name }} </view>

				<view class="content flex just-between align-center">
					<view class="left flex align-base">
						<view class="price-bg flex align-base fBold">
							<text class="f-28 symbol">¥</text>
							<text class="f-44">{{ dataInfo.price }}</text>
						</view>

						<view class="old-price f-24"> ¥{{ dataInfo.original_price }} </view>

						<view class="time-bg flex align-end">
							<image :src="getImageURL('home/detail_time.png')" />
							<view class="f-24"> {{ dataInfo.duration_time }}分钟 </view>
						</view>
					</view>
					<view class="sales f-24"> 超{{ dataInfo.total_sales }}人选择 </view>
				</view>

				<view class="cer flex align-center">
					<image class="logo" :src="getImageURL('home/detail_fxg.png')" />

					<view class="list">
						<authenInfo :showType="2" />
					</view>
				</view>
			</view>

			<view class="wrap-wrap">
				<view
					class="wrap-screen flex"
					id="wrap-screen"
					:style="{ '--screen-top': navigationHeight + 'px' }"
					:class="{ wrapFixed: isFixed }"
				>
					<block v-for="(item, index) in screenList" :key="index">
						<view class="screen-item" @click="onScreen(item)">
							<view class="flex align-center just-center">
								<view
									class="f-28"
									:class="{
										choose: currentScreen === item.id,
									}"
								>
									{{ item.title }}
								</view>
							</view>
						</view>
					</block>
				</view>

				<view class="wrap-scroll">
					<block v-for="(item, index) in screenList" :key="index">
						<view class="scroll-item" :id="item.id">
							<uParse :content="item.content" />
						</view>
					</block>
				</view>
			</view>
		</view>

		<wrapFooter :btnTitle="technicianID === '' ? '选择服务' : '立即下单'" @onClick="onChoose" />
	</div>
</template>

<script>
	import { serviceDetailsData } from "@/API/home.js";
	import banner from "@/components/home/banner.vue";
	import authenInfo from "@/components/home/authenInfo.vue";
	import wrapFooter from "@/components/footer/footer.vue";
	import uParse from "@/components/uParse/uParse.vue";
	export default {
		components: {
			banner,
			authenInfo,
			wrapFooter,
			uParse,
		},
		data() {
			return {
				serviceID: "", //服务ID
				technicianID: "", //技师ID
				dataInfo: {
					images: [],
				}, //服务详情数据

				navigationHeight: this.$navigationHeight,
				screenList: [
					{
						title: "项目介绍",
						id: "screen_1",
						top: 0,
						height: 0,
						content: "<p></p>",
					},
					{
						title: "禁忌说明",
						id: "screen_2",
						top: 0,
						height: 0,
						content: "<p></p>",
					},
					{
						title: "下单须知",
						id: "screen_3",
						top: 0,
						height: 0,
						content: "<p></p>",
					},
				],
				currentScreen: "screen_1",
				isFixed: false,
				isMove: false,
				scrollTop: 0,
				screenTimer: null,
			};
		},
		onLoad(options) {
			this.serviceID = options.id;
			if (options.technicianID) {
				this.technicianID = options.technicianID;
			}
			this.getData();
		},
		onPageScroll(e) {
			if (this.isMove == true) {
				return;
			}
			if (e.scrollTop > this.scrollTop) {
				this.isFixed = true;
				let totalTop = 0;
				for (let i = 0; i < this.screenList.length; i++) {
					let item = this.screenList[i];
					totalTop = totalTop + item.top + item.height;
					if (totalTop > e.scrollTop && e.scrollTop > item.top) {
						this.currentScreen = item.id;
					}
				}
			} else {
				this.isFixed = false;
			}
		},
		methods: {
			/**
			 * 计算节点高度
			 */
			onCalculationHeight() {
				this.screenList.map((item, index) => {
					let query = uni.createSelectorQuery();
					query.select("#screen_" + (index + 1)).boundingClientRect();
					query.exec((res) => {
						item.top = res[0].top - this.navigationHeight - this.$filters.rpxToPx(120);
						item.height = res[0].height;
					});
				});

				let query = uni.createSelectorQuery();
				query.select("#wrap-screen").boundingClientRect();
				query.exec((res) => {
					this.scrollTop = res[0].top - this.navigationHeight;
				});
			},
			/**
			 * 筛选
			 */
			onScreen(item) {
				if (this.currentScreen === item.id) {
					return;
				}
				this.isMove = true;
				let totalTop = 0;
				const index = this.screenList.findIndex((subItem) => {
					return subItem.id === item.id;
				});
				for (let i = 0; i < index; i++) {
					let item = this.screenList[i];
					totalTop = totalTop + item.height;
				}
				if (totalTop > this.scrollTop) {
					if (this.screenList[index].top > this.scrollTop) {
						this.isFixed = true;
					} else {
						this.isFixed = false;
					}
				}
				this.currentScreen = item.id;
				uni.pageScrollTo({
					scrollTop: item.top,
				});
				clearTimeout(this.screenTimer);
				this.screenTimer = setTimeout(() => {
					this.isMove = false;
				}, 1000);
			},
			/**
			 * 选择服务
			 */
			onChoose() {
				if (!this.$filters.isLogin()) {
					return;
				}
				if (this.technicianID === "") {
					uni.navigateTo({
						url: `/subPages/home/chooseTechnician?serviceID=${this.serviceID}`,
					});
				} else {
					this.$filters.goConfirmOrder(this.technicianID, this.serviceID);
				}
			},
			/**
			 * 网络请求
			 */
			getData() {
				serviceDetailsData(this.serviceID).then((res) => {
					const dataInfo = res.data;
					this.dataInfo = dataInfo;
					this.screenList[0].content = dataInfo.project_introduction;
					this.screenList[1].content = dataInfo.taboo_statement;
					this.screenList[2].content = dataInfo.instructions_order;

					this.$nextTick(() => {
						this.onCalculationHeight();
					});
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.wrap {
		--bottom-height: 140rpx;
		width: 100%;
		overflow: hidden;

		.wrap-content {
			width: 100%;
			overflow: hidden;
			background-color: white;
			padding: 30rpx;
			box-sizing: border-box;

			.title {
				color: $title-color;
			}

			.content {
				margin-top: 20rpx;
				width: 100%;

				.left {
					flex: 1;
					margin-right: 10rpx;
					overflow: hidden;

					.price-bg {
						color: $price-color;

						.symbol {
							margin-right: 5rpx;
						}
					}

					.old-price {
						margin-left: 8rpx;
						color: $sub-title-color;
						text-decoration: line-through;
					}

					.time-bg {
						margin-left: 30rpx;
						image {
							width: 32rpx;
							height: 32rpx;
						}
						view {
							margin-left: 10rpx;
							color: $sub-title-color;
						}
					}
				}

				.sales {
					color: $sub-title-color;
				}
			}

			.cer {
				margin-top: 20rpx;
				width: 100%;
				height: 86rpx;
				background: #f3f3fb;
				border-radius: 14rpx;
				overflow: hidden;
				box-sizing: border-box;

				.logo {
					margin-left: 34rpx;
					width: 100rpx;
					height: 38rpx;
				}

				.list {
					margin-left: 30rpx;
					flex: 1;
					overflow: hidden;
				}
			}
		}

		.wrap-wrap {
			margin-top: 20rpx;
			padding-top: 100rpx;
			overflow: hidden;
			position: relative;
		}

		.wrapFixed {
			position: fixed !important;
			top: var(--screen-top) !important;
		}

		.wrap-screen {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			background-color: white;
			height: 100rpx;
			overflow: hidden;

			.screen-item {
				flex: 1;
				display: inline-block;
				height: 100rpx;
				box-sizing: border-box;

				> view {
					width: 100%;
					height: 100%;

					> view {
						height: 100rpx;
						line-height: 100rpx;
						text-align: center;
						box-sizing: border-box;
						border-bottom: 6rpx solid #ffffff;
					}

					.choose {
						color: $theme-color;
						border-bottom-color: $theme-color;
					}
				}
			}
		}

		.wrap-scroll {
			width: 100%;
			overflow: hidden;

			.scroll-item {
				margin-top: 20rpx;
				width: 100%;
				overflow: hidden;
				background: white;
				padding: 30rpx;
				box-sizing: border-box;
			}
		}
	}
</style>
